﻿using CommonLibrary;
using CommonLibrary.Models;
using CommonSite.Areas.API.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BLToolkit.Data.Linq;
using CommonSite.Common;
using CommonHelper;

namespace CommonSite.Areas.API.Controllers
{
    public class DictionaryController : BaseAPIController
    {
        public ActionResult Query()
        {
            using (CommonDatabase db = new CommonDatabase())
            {
                var query = (from p in db.wf_Dictionary
                             where p.State == 1
                             select new M_Dictionary
                             {
                                 Id = CryptHelper.Encrypt(p.Id),
                                 Value = p.Value,
                                 SubGroup = p.SubGroup
                             }).ToList();

                return Success(new
                {
                    Total = query.Count,
                    Rows = query
                });
            }
        }

        public ActionResult Create(M_Dictionary model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        if (db.wf_Dictionary.Where(k =>
                                    k.Value == model.Value &&
                                    k.SubGroup == model.SubGroup &&
                                    k.State == 1)
                              .Count() > 0)
                        {
                            throw new ArgumentException("值不能重复");
                        }

                        db.wf_Dictionary.Insert(() => new wf_Dictionary
                        {
                            Value = model.Value,
                            SubGroup = model.SubGroup
                        });

                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Update(M_Dictionary model)
        {
            if (this.ModelState.IsValid)
            {
                try
                {
                    using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                    {
                        db.BeginTransaction();
                        int id = CryptHelper.DecryptToInt32(model.Id);
                        if (db.wf_Dictionary.Where(k =>
                                    k.State == 1 && k.Id != id &&
                                    k.Value == model.Value &&
                                    k.SubGroup == model.SubGroup)
                             .Count() > 0)
                        {
                            throw new ArgumentException("参数错误");
                        }

                        db.wf_Dictionary.Where(k => k.Id == id)
                          .Set(k => k.Value, model.Value)
                          .Set(k => k.SubGroup, model.SubGroup)
                          .Update();
                        
                        db.CommitTransaction();
                        return Success();
                    }
                }
                catch (Exception ex)
                {
                    return Failed(ex.Message);
                }
            }

            return ArgumentFailed();
        }

        public ActionResult Delete(M_Dictionary model)
        {
            try
            {
                using (CommonDatabase db = new CommonDatabase(CommonOption.ReadWrite))
                {
                    db.BeginTransaction();
                    int id = CryptHelper.DecryptToInt32(model.Id);
                    db.wf_Dictionary.Where(k => k.Id == id)
                      .Set(k => k.State, 0)
                      .Update();

                    db.CommitTransaction();
                    return Success();
                }
            }
            catch (Exception ex)
            {
                return Failed(ex.Message);
            }
        }
    }
}
